package top.infopub.mgr.sys.domain;


import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

import org.apache.commons.beanutils.BeanUtils;

import top.infopub.model.sys.Area;
import top.infopub.model.sys.City;


public class CityDomain extends City {

    private static final long serialVersionUID = -5923332822277488611L;

    private Set<Area> areas = new TreeSet<Area>(new AreaComparator());

    public void addArea(Area area) {
        boolean addFlag = true;
        for (Area var : areas) {
            if (var.getId().equalsIgnoreCase(area.getId())) {
                addFlag = false;
                break;
            }
        }
        if (addFlag) areas.add(area);
    }

    public Set<Area> getListAreas() {
        Set<Area> _tmp = new TreeSet<Area>(new AreaComparator());
        for (Area pro : areas) {
            Area dest = new Area();
            try {
                BeanUtils.copyProperties(dest, pro);
                _tmp.add(dest);
            }
            catch (Exception e) {
                //do nothing
            }
        }
        return _tmp;
    }

    class AreaComparator implements Comparator<Area> {
        @Override
        public int compare(Area o1, Area o2) {
            if (o1.getIsort() == null || o2.getIsort() == null) {
                return -1;
            }
            if (o1.getIsort() > o2.getIsort()) {
                return 1;
            }
            else {
                return -1;
            }
        }
    }

}
